//
//  ZYLFastLookIntroduceView.m
//  Cartoon
//
//  Created by dllo on 15/7/14.
//  Copyright (c) 2015年 dllo. All rights reserved.
//
#import "BaseHeader.h"
#import "ZYLFastLookIntroduceView.h"
#import "ZYLFastLookLTView.h"
#import "UIImageView+WebCache.h"
@interface ZYLFastLookIntroduceView ()
@property (nonatomic, retain) UIImageView *img;
@property (nonatomic, retain) ZYLFastLookLTView *supportLtView;
@property (nonatomic, retain) ZYLFastLookLTView *commentLtView;
@property (nonatomic, retain) UIButton *continueLook;
@property (nonatomic, retain) UIView *garyView;
@property (nonatomic, retain) NSNumber *likes_count;
@property (nonatomic, retain) NSNumber *comments_count;
@property (nonatomic, assign) NSInteger count;
@end
@implementation ZYLFastLookIntroduceView

- (void)dealloc
{
    [_likes_count release];
    [_comments_count release];
    [_titleLabel release];
    [_continueLook release];
    [_commentLtView release];
    [_img release];
    [_supportLtView release];
    [_contentLabel release];
    [_authorName release];
    [_authorhead release];
    [_details release];
    [super dealloc];
}


- (instancetype)initWithFrame:(CGRect)frame dataDic:(NSDictionary *)dataDic
{
    self = [super initWithFrame:frame];
    if (self) {
        self.count = 1;
        [self createHeadImage:dataDic];
        [self createCollect];
        [self createView:dataDic];
        [self backButton];
    }
    return self;
}

/**
 *  头部图片
 */
- (void)createHeadImage:(NSDictionary *)dataDic
{
    self.img = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH * 348 / 640)] autorelease];
    NSString *strUrl = [dataDic objectForKey:@"cover_image_url"];
    [self.img sd_setImageWithURL:[NSURL URLWithString:strUrl] placeholderImage:[UIImage imageNamed:@"place.jpg"]];
    [self addSubview:self.img];
    //赞
    self.supportLtView = [ZYLFastLookLTView ltViewWitnFrame:CGRectMake(SCREEN_WIDTH - 80 * SCREEN_WIDTH / 375, 50 * SCREEN_HEIGHT / 667, 70 * SCREEN_WIDTH / 375, 23 * SCREEN_HEIGHT / 667)];
    self.supportLtView.backgroundColor = [UIColor blackColor];
    self.supportLtView.alpha = 0.3;
    self.supportLtView.layer.cornerRadius = 5;
    self.supportLtView.img.image = [UIImage imageNamed:@"iconfont-zan2"];
    /**
     *  赋值
     */
    self.likes_count = [dataDic objectForKey:@"likes_count"];
    self.supportLtView.myLabel.text = self.likes_count.stringValue;
    [self addSubview:self.supportLtView];
    //评论
    self.commentLtView = [ZYLFastLookLTView ltViewWitnFrame:CGRectMake(self.supportLtView.frame.origin.x, self.supportLtView.frame.origin.y + self.supportLtView.frame.size.height + 20 * SCREEN_HEIGHT / 667, self.supportLtView.frame.size.width, self.supportLtView.frame.size.height)];
    self.commentLtView.backgroundColor = [UIColor blackColor];
    self.commentLtView.alpha = 0.3;
    self.commentLtView.layer.cornerRadius = 5;
    self.commentLtView.img.image = [UIImage imageNamed:@"1"];
    /**
     *  赋值
     */
    self.comments_count = [dataDic objectForKey:@"comments_count"];
    self.commentLtView.myLabel.text = self.comments_count.stringValue;
    [self addSubview:self.commentLtView];
    
    UILabel *titleLabel = [[[UILabel alloc] initWithFrame:CGRectMake(10, self.img.frame.size.height - 20 * SCREEN_HEIGHT / 667, SCREEN_WIDTH - 20 * SCREEN_WIDTH / 375, 20 * SCREEN_HEIGHT / 667)] autorelease];
    /**
     *  赋值
     */
      titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
    titleLabel.text = [dataDic objectForKey:@"title"];
  
    [self.img addSubview:titleLabel];
}

/**
 *  收藏 续看
 */
- (void)createCollect
{
    self.garyView = [[UIView alloc] initWithFrame:CGRectMake(0, self.img.frame.size.height, SCREEN_WIDTH, 50 * SCREEN_HEIGHT / 667)];
    self.garyView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.garyView];
    
    UIButton *collect = [UIButton buttonWithType:UIButtonTypeCustom];
    collect.frame = CGRectMake(40 * SCREEN_WIDTH / 375, 10 * SCREEN_HEIGHT / 667, 100 * SCREEN_WIDTH / 375, 30 * SCREEN_HEIGHT / 667);
    collect.backgroundColor = [UIColor colorWithRed:0.9333 green:0.4471 blue:0.4392 alpha:1];
    collect.layer.cornerRadius = 5;
    [collect setTitle:@"收藏" forState:UIControlStateNormal];
    [self.garyView addSubview:collect];
    [collect addTarget:self action:@selector(collectAction) forControlEvents:UIControlEventTouchUpInside];
    
    self.continueLook = [UIButton buttonWithType:UIButtonTypeCustom];
    self.continueLook.frame = CGRectMake(SCREEN_WIDTH - 40 * SCREEN_WIDTH / 375 - 100 * SCREEN_WIDTH / 375, collect.frame.origin.y, 100 * SCREEN_WIDTH / 375, collect.frame.size.height);
    self.continueLook.backgroundColor = [UIColor colorWithRed:1.0 green:0.698 blue:0.4196 alpha:1];
    self.continueLook.layer.cornerRadius = 5;
    [self.continueLook setTitle:@"续看" forState:UIControlStateNormal];
    [self.garyView addSubview:self.continueLook];
    [self.continueLook addTarget:self action:@selector(continueLookAction) forControlEvents:UIControlEventTouchUpInside];
}

/**
 *  简介
 */
- (void)createView:(NSDictionary *)dataDic
{
    UILabel *ineroduce = [[[UILabel alloc] initWithFrame:CGRectMake(10 * SCREEN_WIDTH / 375, self.garyView.frame.origin.y + self.garyView.frame.size.height, SCREEN_WIDTH - 20 * SCREEN_WIDTH / 375, 20 * SCREEN_HEIGHT / 667)] autorelease];
    ineroduce.text = @"简介";
    [self addSubview:ineroduce];
    
    self.authorhead = [[[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width -  80 * SCREEN_WIDTH / 375, ineroduce.frame.origin.y, 20 * SCREEN_WIDTH / 375, 20 * SCREEN_WIDTH / 375)] autorelease];
    [self addSubview:self.authorhead];
    
    self.authorName = [[[UILabel alloc] initWithFrame:CGRectMake(self.authorhead.frame.origin.x + self.authorhead.frame.size.width + 5, self.authorhead.frame.origin.y, 50 * SCREEN_WIDTH / 375, 20 * SCREEN_HEIGHT / 667)] autorelease];
    [self addSubview:self.authorName];
    self.authorName.font = [UIFont systemFontOfSize:13];
    
    self.contentLabel = [[[UILabel alloc] initWithFrame:CGRectMake(ineroduce.frame.origin.x, ineroduce.frame.origin.y + ineroduce.frame.size.height, ineroduce.frame.size.width, [[self class]heightWithText:self.contentLabel.text])] autorelease];
    self.contentLabel.numberOfLines = 0;
    [self addSubview:self.contentLabel];
    self.contentLabel.text = [dataDic objectForKey:@"description"];
    [self.contentLabel sizeToFit];
    self.contentLabel.font = [UIFont systemFontOfSize:13];
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, self.contentLabel.frame.size.height + self.contentLabel.frame.origin.y + 5, SCREEN_WIDTH, 20 * SCREEN_HEIGHT / 667)];
    headerView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
    
    UILabel *excel = [[[UILabel alloc] initWithFrame:CGRectMake(5 * SCREEN_WIDTH / 375, 2.5 * SCREEN_HEIGHT / 667, 0, 15)] autorelease];
    excel.text = @"专题列表";
    excel.font = [UIFont systemFontOfSize:13];
    [excel sizeToFit];
    [headerView addSubview:excel];
    UIButton *Sequence = [UIButton buttonWithType:UIButtonTypeCustom];
    Sequence.frame = CGRectMake(SCREEN_WIDTH - 35 * SCREEN_WIDTH / 375, 2.5 * SCREEN_HEIGHT / 667, 30 * SCREEN_WIDTH / 375, 15 * SCREEN_WIDTH / 375);
    [Sequence setTitle:@"正序" forState:UIControlStateNormal];
    [Sequence setTitle:@"倒序" forState:UIControlStateSelected];
    [Sequence setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    Sequence.titleLabel.font = [UIFont systemFontOfSize:10];
    [Sequence addTarget:self action:@selector(sequenceAction:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:Sequence];
    [self addSubview:headerView];
}

/**
 *  返回按钮
 */
- (void)backButton
{
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(8 * SCREEN_WIDTH / 375, 27 * SCREEN_WIDTH / 375, 30 * SCREEN_WIDTH / 375, 30 * SCREEN_WIDTH / 375);
    [backButton setImage:[UIImage imageNamed:@"iconfont-fanhui-2"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:backButton];
}



+ (instancetype)introduceWithFrame:(CGRect)frame dataDic:(NSDictionary *)dataDic
{
    id result = [[[self alloc] initWithFrame:frame dataDic:dataDic] autorelease];
    return result;
}

- (void)sequenceAction:(UIButton *)button
{
    self.count++;
    if (self.count % 2 != 0) {
        button.selected = NO;
        [self.lsyDelegate queueButton:@"0"];
        
    } else {
        button.selected = YES;
        [self.lsyDelegate queueButton:@"1"];
    }
}


- (void)backAction
{
    [self.lsyDelegate backButton];
}

/**
 *  收藏 点击方法
 */
- (void)collectAction
{
#warning 专题页面 收藏 按钮 预留 位置
    NSLog(@"收藏");
}

- (void)continueLookAction{
#warning 专题页面 续看 按钮预留位置
    NSLog(@"续看");
}



+ (CGFloat)heightWithText:(NSString *)text

{
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
    CGSize size = CGSizeMake(SCREEN_WIDTH - 25 * SCREEN_WIDTH / 375, 1000);
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.height;
    
}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
